package com.zly.action;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.zly.dao.DormDao;
import com.zly.dao.impl.DormDaoImpl;
import com.zly.entity.User;

public class UserAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String name;
	private String pwd;
	private String upost;
	// 定义返回的结果集 json用的到
	private String result;

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
	public String getUpost() {
		return upost;
	}

	public void setUpost(String upost) {
		this.upost = upost;
	}

	public String login() {
		// 实例化session
		HttpSession session = ServletActionContext.getRequest().getSession();
		// 调用service层实现数据的查询
		DormDao<User> dormDao = new DormDaoImpl<>();
		// 拼接查询语句
		String sql = "select * from user where uname='" + name + "' and upwd = '" + pwd + "' and upost = '"+upost+"'";
		// 调用select方法查询
		List<User> list = (List<User>) dormDao.select(User.class, sql);
		// 判断是否存在 不存在重新登录 存在就把用户名存储到session中并且返回success
		System.out.println(list);
		System.out.println(upost);
		//为了弹出密码或者账号错误
//		HttpServletResponse response=ServletActionContext.getResponse();
//		response.setCharacterEncoding("UTF-8");
//		response.setContentType("text/html;charset:utf-8");
		if (list.isEmpty()) {
			return "login";
		} else {
			session.setAttribute("name", name);
			// System.out.println(name+":"+pwd);
			if ("系统管理员".equals(upost)) {
				return "systemLogin";
			} else if ("宿舍管理员".equals(upost)) {
				return "dormLogin";
			} else if ("学生".equals(upost)) {
				return "studentLogin";
			}
		}
		return null;
	}

	public String loginSuccess() {
		return SUCCESS;
	}
}
